package com.yanqu.road.dao.impl.activity.xiuxian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianEquipmentData;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianMissionData;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianUserData;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianPveUserProperty;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class XiuXianUserDataDaoImpl extends TempDataObjectDao<XiuXianUserData> {

    public static String getXiuXianUserDataSql = "select * from `t_u_activity_xiuxian_data` where `activityId`=? and `userId`=?";

    public XiuXianUserData getXiuXianUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getXiuXianUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parseData(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateXiuXianUserDataSql = "update `t_u_activity_xiuxian_data` set property=?, tripodLv=?,tripodExp=?," +
            "tripodEquipment=?, powerVal=?,maxPowerVal=?,angerVal=?,practiceVal=?," +
            "practiceSpeed=?, breakCount=?,energy=?,lastBreakFailTime=?,lastEnergyRecTime=?,lastMeditateTime=?," +
            "stateId=?,equipmentMap=?,sectId=?,mountId=?,imageId=?,energyUseCount=?,totalUseEnergy=?,luckPoint=?,unreceivedLuckPoint=?,pvpEndRound=?,items=?,drawTimes=? where activityId=? and userId=?" ;
    @Override
    protected boolean updateData(XiuXianUserData xiuXianUserData) {
        xiuXianUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiuXianUserData.getProperty())));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getTripodLv()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getTripodExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiuXianUserData.getTripodEquipment())));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getPowerVal()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getMaxPowerVal()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getAngerVal()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getPracticeVal()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getPracticeSpeed()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getBreakCount()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getLastBreakFailTime()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getLastEnergyRecTime()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getLastMeditateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getStateId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiuXianUserData.getEquipmentMap())));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getSectId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getMountId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getImageId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getEnergyUseCount()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getTotalUseEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getLuckPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getUnreceivedLuckPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getPvpEndRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(xiuXianUserData.getItems())));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getUserId()));
        if (getDb().executeNoneQuery(updateXiuXianUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertXiuXianUserDataSql = "insert into `t_u_activity_xiuxian_data`(activityId,userId,tripodLv,tripodExp,tripodEquipment,powerVal,maxPowerVal,angerVal,practiceVal,practiceSpeed,breakCount," +
            "energy,lastBreakFailTime,lastEnergyRecTime,lastMeditateTime,property,stateId,equipmentMap,sectId,mountId,imageId,energyUseCount,totalUseEnergy,luckPoint,unreceivedLuckPoint,pvpEndRound,items,drawTimes" +
            ") values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(XiuXianUserData xiuXianUserData) {
        xiuXianUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getTripodLv()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getTripodExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiuXianUserData.getTripodEquipment())));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getPowerVal()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getMaxPowerVal()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getAngerVal()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getPracticeVal()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getPracticeSpeed()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getBreakCount()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getLastBreakFailTime()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getLastEnergyRecTime()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getLastMeditateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiuXianUserData.getProperty())));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getStateId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiuXianUserData.getEquipmentMap())));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getSectId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getMountId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getImageId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getEnergyUseCount()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getTotalUseEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getLuckPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getUnreceivedLuckPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserData.getPvpEndRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(xiuXianUserData.getItems())));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserData.getDrawTimes()));
        if (getDb().executeNoneQuery(insertXiuXianUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected XiuXianUserData parseData(ResultSet rs) throws SQLException {
        XiuXianUserData xiuXianUserData = new XiuXianUserData();
        xiuXianUserData.setActivityId(rs.getInt("activityId"));
        xiuXianUserData.setUserId(rs.getLong("userId"));
        xiuXianUserData.setTripodLv(rs.getInt("tripodLv"));
        xiuXianUserData.setTripodExp(rs.getInt("tripodExp"));
        xiuXianUserData.setPowerVal(rs.getLong("powerVal"));
        xiuXianUserData.setMaxPowerVal(rs.getLong("maxPowerVal"));
        xiuXianUserData.setAngerVal(rs.getInt("angerVal"));
        xiuXianUserData.setPracticeVal(rs.getLong("practiceVal"));
        xiuXianUserData.setPracticeSpeed(rs.getInt("practiceSpeed"));
        xiuXianUserData.setBreakCount(rs.getInt("breakCount"));
        xiuXianUserData.setEnergy(rs.getInt("energy"));
        xiuXianUserData.setStateId(rs.getInt("stateId"));
        xiuXianUserData.setLastBreakFailTime(rs.getLong("lastBreakFailTime"));
        xiuXianUserData.setLastEnergyRecTime(rs.getLong("lastEnergyRecTime"));
        xiuXianUserData.setLastMeditateTime(rs.getLong("lastMeditateTime"));
        xiuXianUserData.setSectId(rs.getInt("sectId"));
        xiuXianUserData.setMountId(rs.getInt("mountId"));
        xiuXianUserData.setImageId(rs.getInt("imageId"));
        xiuXianUserData.setEnergyUseCount(rs.getInt("energyUseCount"));
        xiuXianUserData.setTotalUseEnergy(rs.getInt("totalUseEnergy"));
        xiuXianUserData.setLuckPoint(rs.getInt("luckPoint"));
        xiuXianUserData.setUnreceivedLuckPoint(rs.getInt("unreceivedLuckPoint"));
        xiuXianUserData.setPvpEndRound(rs.getInt("pvpEndRound"));
        String value = rs.getString("equipmentMap");
        if(!StringUtils.isNullOrEmpty(value)){
            Map<Integer, XiuXianEquipmentData> tempMap = JSON.parseObject(value, new TypeReference<ConcurrentHashMap<Integer, XiuXianEquipmentData>>(){});
            xiuXianUserData.setEquipmentMap(tempMap);
        }
        xiuXianUserData.setLastMeditateTime(rs.getLong("lastMeditateTime"));
        xiuXianUserData.setItems(PropertyHelper.parseStringToProperty(rs.getString("items")));
        xiuXianUserData.setTripodEquipment(JSON.parseObject(rs.getString("tripodEquipment"), new TypeReference<XiuXianEquipmentData>() {}));
        xiuXianUserData.setProperty(JSON.parseObject(rs.getString("property"), new TypeReference<XiuXianPveUserProperty>() {
        }));
        xiuXianUserData.setDrawTimes(rs.getLong("drawTimes"));
        return xiuXianUserData;
    }
}
